<template>
  <div class="dashboard-container">
    <div class="app-container">
      <template>
        <div class="dashboard-container">
          <div class="app-container">
            <el-card>
              <el-tabs>
                <!-- 放置页签 -->
                <el-tab-pane label="角色管理">
                  <!-- 新增角色按钮 -->
                  <el-row style="height: 60px">
                    <el-button icon="el-icon-plus" size="small" type="primary" @click="addRole">新增角色</el-button>
                  </el-row>
                  <!-- 表格 -->
                  <el-table :data="list" border="">
                    <el-table-column type="index" label="序号" width="120" />
                    <el-table-column prop="name" label="角色名称" width="240" />
                    <el-table-column prop="description" label="描述" />
                    <el-table-column label="操作">
                      <template slot-scope="{ row }">
                        <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                        <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                        <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                  <!-- 分页组件 -->
                  <el-row type="flex" justify="center" align="middle" style="height: 60px">
                    <!-- 分页组件 -->
                    <el-pagination :current-page="page.page" :page-size="page.pagesize" :total="page.total" layout="prev, pager, next" @current-change="changePage" />
                  </el-row>
                </el-tab-pane>
                <el-tab-pane label="公司信息">
                  <el-alert title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" type="info" show-icon :closable="false" />
                  <el-form label-width="120px" style="margin-top: 50px">
                    <el-form-item label="公司名称">
                      <el-input v-model="formData.name" disabled style="width: 400px" />
                    </el-form-item>
                    <el-form-item label="公司地址">
                      <el-input v-model="formData.companyAddress" disabled style="width: 400px" />
                    </el-form-item>
                    <el-form-item label="邮箱">
                      <el-input v-model="formData.mailbox" disabled style="width: 400px" />
                    </el-form-item>
                    <el-form-item label="备注">
                      <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width: 400px" />
                    </el-form-item>
                  </el-form>
                </el-tab-pane>
              </el-tabs>
            </el-card>
          </div>
        </div>
      </template>

      <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree ref="permTree" :data="permData" :props="defaultProps" :show-checkbox="true" :check-strictly="true" :default-expand-all="true" node-key="id" :default-checked-keys="selectCheck" />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRoleById, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
export default {
  name: 'Setting',
  data() {
    return {
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数 分页使用
      },
      formData: {},
      showDialog: false,
      roleForm: {},
      title: '添加角色',
      showPermDialog: false,
      permData: [], // 树形数据
      defaultProps: {
        label: 'name'
      },
      roleId: null,
      selectCheck: []
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfoData()
  },
  methods: {
    async getRoleList() {
      const result = await getRoleList(this.page)
      this.list = result.rows
      this.page.total = result.total
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    async getCompanyInfoData() {
      this.formData = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除改数据')
        await deleteRoleById(id)
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log('error', error)
      }
    },
    btnCancel() {
      this.showDialog = false
      this.roleForm = {}
    },
    async btnOK() {
      if (this.roleForm.id) {
        await updateRole(this.roleForm)
      } else {
        await addRole(this.roleForm)
        console.log('点击添加')
      }
      this.$message.success('操作成功')
      this.getRoleList()
      this.showDialog = false
    },
    async editRole(id) {
      this.title = '编辑角色'
      this.roleId = id // 将id 保留下来
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    addRole() {
      this.showDialog = true
      this.title = '添加角色'
    },
    async assignPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0')

      // 获取该角色已经的权限点
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPermDialog = true
    },
    async btnPermOK() {
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('权限分配成功')
      this.showPermDialog = false
    },
    btnPermCancel() {
      this.selectCheck = []
      this.showPermDialog = false
    }
  }
}
</script>

<style></style>
